package com.joesem.sale.client.logic.workOrder.details

import android.os.Bundle
import android.view.LayoutInflater
import com.bumptech.glide.Glide
import com.jiaxing.sale.client.databinding.FragmentOrderDetailsContentBinding
import com.jiaxing.sale.client.databinding.ItemImgBinding
import com.joesem.sale.client.base.BaseFragment
import com.joesem.sale.client.base.gone
import com.joesem.sale.client.base.visible
import com.joesem.sale.client.network.data.OrderDetailsMode
import com.joesem.sale.client.utils.click
import com.joesem.sale.client.widget.dialog.imagePreview


class OrderDetailsContentFragment : BaseFragment<FragmentOrderDetailsContentBinding>() {

    override fun FragmentOrderDetailsContentBinding.initView(savedInstanceState: Bundle?) {

    }

    override fun FragmentOrderDetailsContentBinding.initListener() {
    }

    fun refresh(data: OrderDetailsMode?) {
        bind.apply {
            data?.let { it ->
                tvOrderNo.text = it.wo_number.orEmpty()
                tvOrderType.text = it.type
                tvProduct.text =
                    it.products?.joinToString { it.product?.name.orEmpty() + if (it.product?.model.isNullOrEmpty()) "" else "-${it.product?.model}" }
                tvServiceContent.text = it.service_content.orEmpty()
                tvMsg.text = it.description.orEmpty()
                if (it.images.isNullOrEmpty()) {
                    llImgTitle.gone()
                    llImage.gone()
                } else {
                    llImage.visible()
                    llImgTitle.visible()
                    llImage.removeAllViews()
                    it.images.forEachIndexed { index, image ->
                        val bindItem = ItemImgBinding.inflate(LayoutInflater.from(requireContext()))
                        bindItem.btnImgDelete.gone()
                        bindItem.root.click { v ->
                            imagePreview(
                                requireContext(),
                                childFragmentManager,
                                it.images.map { it?.img_url.orEmpty() },
                                index
                            )
                        }
                        Glide.with(requireContext())
                            .load(image?.img_url)
                            .into(bindItem.btnImg)
                        llImage.addView(bindItem.root)
                    }
                }
                tvCreateUser.text = it.create_user?.username.orEmpty()
                tvCreateDate.text = it.create_time_format.orEmpty()
            }
        }
    }
}